package com.user.query.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.user.query.model.AdminArea;
import com.user.query.service.AdminAreaService;
import com.user.query.utils.HttpCode;
import com.user.query.utils.IDGenerator;
import com.user.query.utils.Page;

/**
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

@Controller
@RequestMapping("/area")
public class AdminAreaController extends BaseController{
 
	@Resource
	private AdminAreaService adminAreaService;
	
	@RequestMapping("/list")
	public String defaults(@RequestParam(defaultValue = "1",required = false)Integer pageNo,
			@RequestParam(value="keyWord",required = false)String keyWord,ModelMap map){
		try {
			Page page= new Page();
    		page.setPageNo(pageNo);
    		int count = adminAreaService.countAllByPage(keyWord);
    		if(count>0){
    			List<AdminArea> adminAreas = adminAreaService.findAllByPage(keyWord, page);
    			map.put("adminAreas", adminAreas);
    		}
    		page.setTotalRecord(count);
    		map.put("page",page);
    		
    		if(StringUtils.isNotBlank(keyWord)){
    			map.put("keyWord",keyWord);
    		}
    		
    		map.put("code", HttpCode.HTTP_CODE_200);
		} catch (Exception e) {
			map.put("code", HttpCode.HTTP_CODE_500);
	        map.put("msg", "查询行政区域列表失败");
		    log.error("查询行政区域列表失败",e);
		}
		return "system/area";  //初始化页面
	}
	
	/**
	 * 
	 * @Description: 新增区域 
	 * @author  zhaihaixu
	 * @date 2015-10-27 下午11:08:09
	 */
	@RequestMapping("/add")
    @ResponseBody
	public Map<String, Object> add(@RequestParam(value = "areaNo") String areaNo,
			@RequestParam(value = "areaName") String areaName,
			@RequestParam(value = "areaLevel") Integer areaLevel){
		Map<String, Object> map =  new HashMap<String, Object>();
		try {
			AdminArea area =adminAreaService.findByAreaNo(areaNo);
			if(area !=null){
				map.put("code", HttpCode.HTTP_CODE_400);
				map.put("msg", "此区域编码已经存在,请核实!");
				return map;
			}else{
				AdminArea adminArea= new AdminArea();
				adminArea.setAreaId(IDGenerator.getUUID());
				adminArea.setAreaNo(areaNo);
				adminArea.setAreaName(areaName);
				adminArea.setAreaLevel(areaLevel);
				if(areaLevel==2){
					adminArea.setTypeName("市");
					adminArea.setTopNo(areaNo.substring(0, 1)+"0000000000");
				}
				if(areaLevel==3){
					adminArea.setTypeName("区");
					adminArea.setTopNo(areaNo.substring(0, 3)+"00000000");
				}
				if(areaLevel==4){
					adminArea.setTypeName("镇");
					adminArea.setTopNo(areaNo.substring(0, 5)+"000000");
				}
				if(areaLevel==5){
					adminArea.setTypeName("村");
					adminArea.setTopNo(areaNo.substring(0, 8)+"000");
				}
				adminAreaService.insert(adminArea);
				map.put("code", HttpCode.HTTP_CODE_200);
			}
			
		} catch (Exception e) {
			map.put("code", HttpCode.HTTP_CODE_500);
	        map.put("msg", "添加区域失败！");
		    log.error("添加区域失败！",e);
		}
		return map;
	}
	
	//查询行政区域
	@RequestMapping("/listAll")
    @ResponseBody
	public  Map<String, Object> listAll(){
		Map<String, Object> map =  new HashMap<String, Object>();
		try {
			 List<AdminArea>  adminArea =  adminAreaService.findAll();
			 map.put("adminArea", adminArea);
			 map.put("code", HttpCode.HTTP_CODE_200);
		} catch (Exception e) {
			map.put("code", HttpCode.HTTP_CODE_500);
	        map.put("msg", "查询行政区域信息失败");
		    log.error("查询行政区域信息失败",e);
		}
		return map;
	}
	
	//编辑页面  
	@RequestMapping("/preUpdate")
    @ResponseBody
	public  Map<String, Object> preUpdate(@RequestParam("areaId") String areaId){
		Map<String, Object> map =  new HashMap<String, Object>();
		try {
			 AdminArea  adminArea =  adminAreaService.selectByPrimaryKey(areaId);
			 map.put("adminArea", adminArea);
			 map.put("code", HttpCode.HTTP_CODE_200);
		} catch (Exception e) {
			map.put("code", HttpCode.HTTP_CODE_500);
	        map.put("msg", "获取该条区域信息失败");
		    log.error("获取该条区域信息失败",e);
		}
		return map;
	}
	//编辑更新
	@RequestMapping("/update")
    @ResponseBody
	public  Map<String, Object> update(@RequestParam(value="areaId")String areaId,
			@RequestParam(value = "areaNo") String areaNo,
			@RequestParam(value = "areaName") String areaName,
			@RequestParam(value = "areaLevel") Integer areaLevel){
		Map<String, Object> map =  new HashMap<String, Object>();
		try {
			AdminArea adminArea =new AdminArea();
			adminArea.setAreaId(areaId);
			adminArea.setAreaNo(areaNo);
			adminArea.setAreaName(areaName);
			adminArea.setAreaLevel(areaLevel);
			adminAreaService.updateByPrimaryKeySelective(adminArea);
			map.put("code", HttpCode.HTTP_CODE_200);
		} catch (Exception e) {
			map.put("code", HttpCode.HTTP_CODE_500);
	        map.put("msg", "更新区域信息失败");
		    log.error("更新区域信息失败：id="+areaId+":::",e);
		}
		return map;
	}
	/**
	 * 
	 * 方法含义：获取country
		@author bgt
		@return 
		@date 2015-11-26
	 */
	@RequestMapping("/getCounty")
    @ResponseBody
	public Map<String, Object>  getCounty(@RequestParam("cityId")String cityId){
		Map<String, Object> map =  new HashMap<String, Object>();
		try {
			List<AdminArea> countys = adminAreaService.findAllByToNo(cityId);
			map.put("data", countys);
			map.put("code", HttpCode.HTTP_CODE_200);
			map.put("msg","查找成功");
		} catch (Exception e) {
			map.put("code", HttpCode.HTTP_CODE_500);
	        map.put("msg", "查询县级市失败！");
		    log.error("查询县级市失败",e);
		}
		return map;
	}
	
		/**
		 * 查询市
		 * 方法含义：
			@author bgt
			@return 
			@date 2015-11-26
		 */
		@RequestMapping("/getCity")
		@ResponseBody
		public Map<String, Object> getCity() {
			Map<String, Object> returnMap = new HashMap<String, Object>();
			try {
				int level = 2;
				//2级区域
				List<AdminArea> adminAreas = adminAreaService.findAllByLevel2(level);
				//returnMap.put("adminAreas", adminAreas);
				returnMap.put("code", HttpCode.HTTP_CODE_200);
				returnMap.put("msg", "查找成功");
				returnMap.put("data", adminAreas);
				return returnMap;
			} catch (Exception e) {
				log.error("查询市失败", e);
				returnMap.put("code", HttpCode.HTTP_CODE_500);
				returnMap.put("msg", "查找失败");
				return returnMap;
			}
		}
}
